package com.yl.order.model;

import com.yl.order.Constants;
import com.yl.order.db.DbManage;
import com.yl.order.db.entity.CategoryEntity;
import com.yl.order.db.entity.GoodsEntity;
import com.yl.order.db.entity.ProductEntity;
import com.yl.order.db.entity.SpecEntity;
import com.yl.order.db.CategoryEntityDao;
import com.yl.order.db.GoodsEntityDao;
import com.yl.order.db.MqttHistoryEntityDao;
import com.yl.order.db.ProductEntityDao;
import com.yl.order.db.SpecEntityDao;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.Observable;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * create by cjx on 2024/4/21
 * desc:
 */
public class SyncModel {

    private static ArrayList<CategoryEntity> productCategoryList = null;
    private static ArrayList<CategoryEntity> specCategoryList = null;

    // 分组-规格
    private static Map<String, List<SpecEntity>> specMap = null;
    // 分组-产品
    private static HashMap<String, ArrayList<GoodsEntity>> goodsMap = null;
    // 分组-产品
    private static HashMap<String, ArrayList<ProductEntity>> productMap = null;

    public SyncModel() {

    }

    public static <T> void deleteItem(T item) {
        DbManage.getInstance().getDaoSession().delete(item);
    }

    public static void execute(String sql) {
        DbManage.getInstance().executeSql(sql);
    }

    public static <T> void insertItem(T item) {
        DbManage.getInstance().getDaoSession().insert(item);
    }

    public static <T> void updateItem(T item) {
        DbManage.getInstance().getDaoSession().update(item);
    }

    public synchronized static ArrayList<CategoryEntity> getSpecCategoryList() {
        if (specCategoryList == null) {
            CategoryEntityDao categoryEntityDao = DbManage.getInstance().getDaoSession().getCategoryEntityDao();
            List<CategoryEntity> list = categoryEntityDao.queryBuilder()
                    .where(CategoryEntityDao.Properties.Type.eq(Constants.GROUP_TYPE_SPEC))
                    .orderAsc(CategoryEntityDao.Properties.Id, CategoryEntityDao.Properties.Sort)
                    .list();
            specCategoryList = new ArrayList<>(list);
        }
        return specCategoryList;
    }

    public synchronized static ArrayList<CategoryEntity> getProductCategoryList() {
        if (productCategoryList == null) {
            CategoryEntityDao categoryEntityDao = DbManage.getInstance().getDaoSession().getCategoryEntityDao();
            List<CategoryEntity> list = categoryEntityDao.queryBuilder()
                    .where(CategoryEntityDao.Properties.Type.eq(Constants.GROUP_TYPE_GOODS))
                    .orderAsc(CategoryEntityDao.Properties.Sort)
                    .list();
            productCategoryList = new ArrayList<>(list);
        }
        return productCategoryList;
    }

    // 重置分类
    public synchronized static void resetCategoryList(ArrayList<CategoryEntity> list) {
        productCategoryList = new ArrayList<>();
        specCategoryList = new ArrayList<>();
        for (CategoryEntity c : list) {
            if (Constants.GROUP_TYPE_GOODS == c.getType()) {
                productCategoryList.add(c);
            } else if (Constants.GROUP_TYPE_SPEC == c.getType()) {
                specCategoryList.add(c);
            }
        }
        DbManage.getInstance().getDaoSession().getCategoryEntityDao().deleteAll();
        DbManage.getInstance().getDaoSession().getCategoryEntityDao().insertInTx(list);
    }

    public synchronized static List<SpecEntity> getSpecList(String groupNumber) {
        if (specMap == null) {
            specMap = new HashMap<>();
        }
        List<SpecEntity> specList = specMap.get(groupNumber);
        if (specList != null) {
            return specList;
        }
        List<SpecEntity> list = DbManage.getInstance().getDaoSession().getSpecEntityDao()
                .queryBuilder()
                .where(SpecEntityDao.Properties.GroupNumber.eq(groupNumber))
                .orderAsc(SpecEntityDao.Properties.Sort)
                .build()
                .list();
        specMap.put(groupNumber, list);
        return list;
    }

    public static List<SpecEntity> getSpecListByNumbers(Object[] numbers) {
        return DbManage.getInstance().getDaoSession().getSpecEntityDao()
                .queryBuilder()
                .where(SpecEntityDao.Properties.Number.in(numbers))
                .build()
                .list();
    }

    // 重置规格
    public static void resetSpecList(ArrayList<SpecEntity> list) {
        specMap = new HashMap<>();
        Collections.sort(list, new Comparator<SpecEntity>() {
            @Override
            public int compare(SpecEntity o1, SpecEntity o2) {
                return o1.getSort() - o2.getSort();
            }
        });
        for (SpecEntity s : list) {
            List<SpecEntity> specEntities = specMap.get(s.getGroupNumber());
            if (specEntities == null) {
                specEntities = new ArrayList<>();
                specMap.put(s.getGroupNumber(), specEntities);
            }
            specEntities.add(s);
        }
        DbManage.getInstance().getDaoSession().getSpecEntityDao().deleteAll();
        DbManage.getInstance().getDaoSession().getSpecEntityDao().insertInTx(list);
    }

    public static GoodsEntity getGoodsByNumber(String goodsNumber) {
        return DbManage.getInstance().getDaoSession().getGoodsEntityDao()
                .queryBuilder()
                .where(GoodsEntityDao.Properties.Number.eq(goodsNumber))
                .build()
                .unique();
    }

    public static void getAllGoodsList(Consumer<ArrayList<GoodsEntity>> onNext) {
        Disposable subscribe = Observable.create((ObservableOnSubscribe<ArrayList<GoodsEntity>>) emitter -> {
                    GoodsEntityDao dao = DbManage.getInstance().getDaoSession().getGoodsEntityDao();
                    List<GoodsEntity> list = dao.queryBuilder()
                            .orderAsc(GoodsEntityDao.Properties.Sort)
                            .build()
                            .list();
                    emitter.onNext(new ArrayList<>(list));
                    emitter.onComplete();
                })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(onNext);
    }

    public static ArrayList<GoodsEntity> getGoodsList(String groupNumber) {
        if (goodsMap == null) {
            goodsMap = new HashMap<>();
        }
        ArrayList<GoodsEntity> productList = goodsMap.get(groupNumber);
        if (productList == null) {
            GoodsEntityDao dao = DbManage.getInstance().getDaoSession().getGoodsEntityDao();
            List<GoodsEntity> list = dao.queryBuilder()
                    .where(GoodsEntityDao.Properties.GroupNumber.eq(groupNumber))
                    .orderAsc(GoodsEntityDao.Properties.Sort)
                    .build()
                    .list();
            ArrayList<GoodsEntity> arrayList = new ArrayList<>(list);
            goodsMap.put(groupNumber, arrayList);
            productList = arrayList;
        }
        return productList;
    }

    // 重置商品
    public static void resetGoodsList(ArrayList<GoodsEntity> list) {
        goodsMap = new HashMap<>();
        for (GoodsEntity g : list) {
            ArrayList<GoodsEntity> goodsEntities = goodsMap.get(g.getGroupNumber());
            if (goodsEntities == null) {
                goodsEntities = new ArrayList<>();
                goodsMap.put(g.getGroupNumber(), goodsEntities);
            }
            goodsEntities.add(g);
        }
        DbManage.getInstance().getDaoSession().getGoodsEntityDao().deleteAll();
        DbManage.getInstance().getDaoSession().getGoodsEntityDao().insertInTx(list);
    }

    public static void updateGoodsList(ArrayList<GoodsEntity> updateList) {
        DbManage.getInstance().getDaoSession().getGoodsEntityDao().updateInTx(updateList);
    }

    public static List<ProductEntity> getAllProductList() {
        return DbManage.getInstance().getDaoSession().getProductEntityDao().loadAll();
    }

    public static ArrayList<ProductEntity> getProductList(String goodsNumber) {
        if (productMap == null) {
            productMap = new HashMap<>();
        }
        ArrayList<ProductEntity> productSpecList = productMap.get(goodsNumber);
        if (productSpecList == null) {
            ProductEntityDao dao = DbManage.getInstance().getDaoSession().getProductEntityDao();
            List<ProductEntity> list = dao.queryBuilder()
                    .where(ProductEntityDao.Properties.GoodsNumber.eq(goodsNumber))
                    .orderAsc(ProductEntityDao.Properties.Sort)
                    .build()
                    .list();
            ArrayList<ProductEntity> arrayList = new ArrayList<>(list);
            productMap.put(goodsNumber, arrayList);
            productSpecList = arrayList;
        }

        return productSpecList;
    }

    public static ProductEntity getProductByGoodsSpec(String goodsNumber, String specNumber) {
        return DbManage.getInstance().getDaoSession().getProductEntityDao()
                .queryBuilder()
                .where(ProductEntityDao.Properties.SpecNumber.eq(specNumber))
                .where(ProductEntityDao.Properties.GoodsNumber.eq(goodsNumber))
                .unique();
    }

    // 批量插入产品信息
    public static void resetProductList(ArrayList<ProductEntity> list) {
        productMap = new HashMap<>();
        for (ProductEntity p : list) {
            ArrayList<ProductEntity> productEntities = productMap.get(p.getGoodsNumber());
            if (productEntities == null) {
                productEntities = new ArrayList<>();
                productMap.put(p.getGoodsNumber(), productEntities);
            }
            productEntities.add(p);
        }
        DbManage.getInstance().getDaoSession().getProductEntityDao().deleteAll();
        DbManage.getInstance().getDaoSession().getProductEntityDao().insertInTx(list);
    }

    // 批量更新分组
    public static void updateGroupList(ArrayList<CategoryEntity> updateList) {
        DbManage.getInstance().getDaoSession().getCategoryEntityDao().updateInTx(updateList);
    }

}
